package lerry.load.loader;

import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.Function;
import flash.utils.JSON;
import flash.utils.Object;
class DataLoader extends EventDispatcher{
	var loader:URLLoader;
	var cb:Function;
	var data:Dynamic;
	public function new() {
		super();
		loader=new URLLoader();
	}
	public function load(url:String,?cb:Function):Void {
		this.cb=cb;
		loader.load(new URLRequest(url));
		loader.dataFormat=URLLoaderDataFormat.TEXT;
		loader.addEventListener(Event.COMPLETE,onCom);
	}
	
	function onCom(event:Event):Void {
		loader.removeEventListener(Event.COMPLETE,onCom);
		data = loader.data;
		if(cb!=null)cb();
		dispatchEvent(new Event(Event.COMPLETE));
	}
	public function getData(){
		return loader.data;
	}
	public function dispose():Void{
		cb=null;
		if(loader!=null){
			loader.close();
			loader=null;
		}
	}
	public function parseJSON():Object{
		trace("string:"+data);
		return JSON.parse(data);
	}
}
